package com.stenio.fancat.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.stenio.fancat.vo.Result;

/**
 * 统一响应格式工具类
 *
 */
public class ResultUtil {

	private static String code = "0";
	private static String msg = "成功";
	private static String LIST = "list";

	/**
	 * 请求成功返回结果，无业务数据
	 *
	 * @return
	 */
	public static Result success() {
		return Result.success(null);
	}

	/**
	 * 请求成功返回结果，包含请求的业务数据
	 *
	 * @param data
	 * @return
	 */
	public static Result success(Object data) {
		return Result.success(data);
	}

	/**
	 * 通过mybatisplus转换为分页结果实体
	 * 
	 * @param pageInfo
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static Result success(com.baomidou.mybatisplus.plugins.Page pageInfo) {
		if (null == pageInfo) {
			return Result.success(null);
		}
		Result result = new Result();
		// 设置分页信息
		Page page = new Page();
		page.setCurrentPageNo(pageInfo.getCurrent());
		page.setPageSize(pageInfo.getSize());
		page.setTotal(pageInfo.getTotal());
		page.setTotalPages(pageInfo.getPages());
		// page.setPageSize(pageInfo.getRecords().size());
		result.setCode(code);
		result.setMsg(msg);
		result.setPage(page);
		Map<String, List> map = new HashMap<String, List>();
		map.put(LIST, pageInfo.getRecords());
		result.setContent(map);
		return result;
	}

	/**
	 * 请求失败返回结果，无业务数据
	 *
	 * @return
	 */
	public static Result error(String code, String msg) {
		return Result.error(code, msg);
	}

}
